﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GdHotel.Data.Interfaces;
using GdHotel.Web.Attributes;
using GdHotel.Web.Models;
using UniqueTechnologies.Attributes;

namespace GdHotel.Web.Controllers
{
    [LayoutSupport]
    public class RoomsController : BaseUiController
    {
        private const String CommonKey = "common";
        private const String RoomsKey = "rooms";
        private const String RoomsExtraKey = "roomsextra";

        public override ILanguagesRepository LanguagesRepository { get; set; }
        public override INotesRepository NotesRepository { get; set; }
        public override IMainMenuItemsRepository MainMenuItemsRepository { get; set; }
        public override IAdditionalMenuItemsRepository AdditionalMenuItemsRepository { get; set; }
        public IRoomsRepository RoomsRepository { get; set; }
        public IRoomTypesRepository RoomTypesRepository { get; set; }
        public IGalleriesRepository GalleriesRepository { get; set; }
        public ICustomPagesRepository CustomPagesRepository { get; set; }

        public RoomsController(
            ILanguagesRepository languagesRepository, 
            INotesRepository notesRepository, 
            IMainMenuItemsRepository mainMenuItemsRepository, 
            IAdditionalMenuItemsRepository additionalMenuItemsRepository,
            IRoomsRepository roomsRepository,
            IRoomTypesRepository roomTypesRepository,
            IGalleriesRepository galleriesRepository,
            ICustomPagesRepository customPagesRepository)
        {
            LanguagesRepository = languagesRepository;
            NotesRepository = notesRepository;
            MainMenuItemsRepository = mainMenuItemsRepository;
            AdditionalMenuItemsRepository = additionalMenuItemsRepository;
            RoomsRepository = roomsRepository;
            RoomTypesRepository = roomTypesRepository;
            GalleriesRepository = galleriesRepository;
            CustomPagesRepository = customPagesRepository;
        }

        //
        // GET: /Rooms/

        public ActionResult Index()
        {
            var roomTypes = RoomTypesRepository
                .FindAllByLang(Language)
                .ToList();
            var gallery = GalleriesRepository.FindAllByKey(RoomsKey).FirstOrDefault();
            var commonGallery = GalleriesRepository.FindAllByKey(CommonKey).FirstOrDefault();
            var page = CustomPagesRepository.FindAllByLangAndKey(Language, RoomsKey).FirstOrDefault();
            var additionalPage = CustomPagesRepository.FindAllByLangAndKey(Language, RoomsExtraKey).FirstOrDefault();

            var model = new RoomsModel { RoomTypes = roomTypes, Gallery = gallery, PageGallery = commonGallery, Page = page, AdditionalPage = additionalPage };
            return View(model);
        }

        public ActionResult Show(Int32 id)
        {
            var room = RoomsRepository.GetById(id);
            if (room == null || room.LanguageId != Language.Id)
                return RedirectToAction("Index");

            var roomTypes = RoomTypesRepository
                .FindAllByLang(Language)
                .ToList();

            var gallery = GalleriesRepository.FindAllByKey(room.Key).FirstOrDefault();
            var commonGallery = GalleriesRepository.FindAllByKey(CommonKey).FirstOrDefault();
            var rooms = room.RoomType.Rooms.OrderBy(r => r.Id);

            var model = new RoomModel {Room = room, RoomTypes = roomTypes, Gallery = gallery, PageGallery = commonGallery, OtherRooms = rooms};
            return View(room.RoomType.Vip ? "ShowVip" : "Show", model);
        }
    }
}
